/**
 * @author GodSon
 * http://www.btboys.com
 */

/**
 * @param {Object} options
 * return windowId
*/
(function($){
    
    /**
	 * 创建UUID
	 */
    function S4() {
        return (((1+Math.random())*0x10000)|0).toString(16).substring(1);
    }
    /**
	 * 生成windowId
	 */
    function  CreateIndentityWindowId(){
        return "UUID-"+(S4()+S4()+"-"+S4()+"-"+S4()+"-"+S4()+"-"+S4()+S4()+S4());
    }
	
    function HasOpen(title){
        var allOpen = $('.appToolbar');
        var hasapp = false;
        allOpen.each(function(i,v){
            if($(this).text() == title){
                hasapp = this.id;
            }
        });
        return hasapp;
    }
	
    //入口方法
    $.fn.openApp = function(){
        var windowId = CreateIndentityWindowId();
        var app = $(this).data("app");
        var ishase = HasOpen(app.text);
        if(ishase){
            $("#"+ishase.split("_")[1]).dialog('open')
        }else{
            var dialog = $('<div id="'+windowId+'"></div>');
            $('.GD_windBody').append(dialog);
            var oapps = $('.GD_windBody > div.window');

            var T = oapps.length*25+10;
            var L = oapps.length*25+300;
                        
            var dconif = {
                title:app.text,
                height:400,
                width:700,
                top:T,
                left:L,
                resizable:true,
                maximizable:true,
                minimizable:true,
                inline:true,
                cache:false,
                shadow:false,
                onClose:function(){
                    $(this).dialog("destroy");
                    $('#App_'+this.id).remove();
                    RIA.getNs("os").ChangeToolbarWinList("");
                },
                onMinimize:function(){
                    if($(this).prev('.window-header').find('.panel-tool-restore').length == 1){
                        var opts = $.data(this, 'panel').options;
                        opts.maximized = true;
                    }
                    $('#App_'+this.id).attr('state','min');
                },
                onMove:function(left,top){
                    var opts = $.data(this, 'panel').options;
                    if(top < 0){
                        $(this).dialog("move",{
                            "left":left,
                            "top":0
                        });
                        $(this).dialog("maximize");
                    }else if(opts.maximized){
                        $(this).dialog("restore");
                        $(this).dialog("move",{
                            "left":left+100,
                            "top":top
                        });
                    }
                    if(top > $('.GD_windBody').height()){
                        $(this).dialog("move",{
                            "left":left,
                            "top":($('.GD_windBody').height()-25)
                            });
                    }
                }
            };
            if(app.href && app.href.indexOf('http://') == -1){
                dconif.href = app.href;
            }
            dialog.dialog(dconif);
                        
            if(app.href && app.href.indexOf('http://') != -1){
                dialog.find('.dialog-content').html('<iframe name="appLink" width="100%" height="100%" frameborder="0" src="'+app.href+'" ></iframe>');
            }
                         
            $(dialog).click(function(){
                $(this).dialog('open')
                });
			
            var appFoot = $('<div class="appToolbar_Foot" id="App_'+windowId+'" state = "open"></div>').click(function(){
                var opts = $.data($("#"+this.id.split("_")[1])[0], 'panel').options;
                var panel = $.data($("#"+this.id.split("_")[1])[0], 'panel').panel;
                if($(this).attr('state') == "min"){
                    panel.show();
                    opts.minimized = false;
                    $(this).attr('state','open');
                }else{
                    panel.hide();
                    opts.minimized = true;
                    $(this).attr('state','min');
                }					
            });
            var tool = $("<div id='Menu_"+windowId+"' class='appToolbar'>"+app.text+"</div>").hover(function(){
                $(this).addClass("appToolbar-hover");
            },function(){
                $(this).removeClass("appToolbar-hover");
            });
			
            appFoot.append(tool);
            $("#GD_WinList").append(appFoot);
        }
    };
})(jQuery);
